package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.queryObject.BannerQueryObject;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;


    /**
     * 分页映射方法
     * @param model
     * @param qo    分页条件
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo")BannerQueryObject qo){
        // Page : spring-data 提供用于 mongodb 分页的类
        Page<Banner> page = bannerService.query(qo);
        model.addAttribute("page",page);
        return "banner/list";
    }


    /**
     * 保存或更新
     * @param banner    要保存的数据
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        if (banner.getId()!=null){
            // 如果id不为空,那么作更新操作
            bannerService.update(banner);
        }else {
            // 保存
            bannerService.save(banner);
        }
        return JsonResult.success();
    }


    /**
     * 删除方法
     * @param id    要删除区域的id
     * @return  JsonResult
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        bannerService.delete(id);
        return JsonResult.success();
    }

    /**
     *
     * @return
     */
    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType(){

        List<Strategy> strategies = strategyService.list();
        List<Travel> travels = travelService.list();

        Map<String,Object> map = new HashMap<>();
        map.put("sts", strategies);
        map.put("ts", travels);

        return JsonResult.success(map);
    }


}
